package com.ingenic.glass.camera.gallery;

import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.LinearLayout;
import com.ingenic.glass.camera.R;
import android.util.Log;
import android.media.ThumbnailUtils;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import android.graphics.drawable.BitmapDrawable;
import android.graphics.Shader.TileMode;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.Video;
public class GalleryAdapter extends BaseAdapter {
	private Activity mContext;
	private ArrayList<Item> mList;
        private final int IMAGE = 1;
        private final int VIDEO = 2;
        private final int SYSTEM = 3;
	private ArrayList<Bitmap> mReleaseBitMapList = new ArrayList<Bitmap>();

        public GalleryAdapter(Activity context, ArrayList<Item> list) {
		// TODO Auto-generated constructor stub
		mContext = context;
		mList = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.gallery, null);
			holder.create_date = (TextView) convertView
					.findViewById(R.id.create_date);
			holder.media_id = (TextView) convertView
					.findViewById(R.id.media_id);
			holder.thumbnail = (ImageView) convertView
					.findViewById(R.id.thumbnail);
			holder.video_mark = (ImageView) convertView
					.findViewById(R.id.video_mark);
			holder.switchLayout = (LinearLayout) convertView
			                .findViewById(R.id.switch_layout);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}	
		boolean fromCamera = mContext.getIntent().getBooleanExtra("from_camera",false); 
		if (fromCamera && position == 0){
		    holder.switchLayout.setVisibility(View.VISIBLE);
		    holder.thumbnail.setVisibility(View.INVISIBLE);
		}else{
		    holder.switchLayout.setVisibility(View.INVISIBLE);
		    holder.thumbnail.setVisibility(View.VISIBLE);
		    int windowsHeight = mContext.getResources().getDisplayMetrics().heightPixels;
		    if (windowsHeight >= 480) {
			holder.create_date.setTextSize(50);
		} else {
			holder.create_date.setTextSize(25);
		}
		if(null!=mList.get(position).Id)
		    holder.media_id.setText(mList.get(position).Id);
		if(null!=mList.get(position).DateTaken)
		    holder.create_date.setText(mList.get(position).DateTaken);		
		if (mList.get(position).MimeType.indexOf("video/") != -1){
		    holder.video_mark.setVisibility(View.VISIBLE);
		}
		else{ 
		    holder.video_mark.setVisibility(View.GONE);
		    //holder.create_date.setVisibility(View.VISIBLE);
		}
		holder.create_date.setVisibility(View.GONE);
		Bitmap bitmap = getThumbnail(mList.get(position).MimeType
					     ,Integer.parseInt(mList.get(position).Id)
					     ,mList.get(position).DataPath);
		if (bitmap == null){
		    if (mList.get(position).MimeType.indexOf("video/") != -1)
			holder.thumbnail.setImageResource(R.drawable.ic_launcher_gallery480);		
		    else
			holder.thumbnail.setImageResource(R.color.white_color);	
		}else {
			holder.thumbnail.setImageBitmap(bitmap);
		    }
		
		mReleaseBitMapList.add(bitmap);			
		}
		return convertView;
	}

	private class ViewHolder {
		private TextView create_date;
		private TextView media_id;
		private ImageView thumbnail;
		private ImageView video_mark;
	        private LinearLayout switchLayout;
	}
    
    private Bitmap getThumbnail(String miniType,int id,String dataPath){
	   Bitmap bitmap = null;
	   BitmapFactory.Options options = new BitmapFactory.Options();
	   //options.inDither = true;
	   options.inPreferredConfig = Bitmap.Config.ARGB_8888;
	   options.inPurgeable = true;
	   options.inInputShareable = true;
	   if (miniType.indexOf("video/") != -1){
	       if (dataPath.startsWith("/system/media/"))		   
		   bitmap = ThumbnailUtils.createVideoThumbnail(dataPath,
								Video.Thumbnails.MINI_KIND);
	       else 
		   bitmap = BitmapManager.instance().getThumbnail(mContext.getContentResolver(), id,
								  Images.Thumbnails.MINI_KIND,options,true);
	   } else 
	       bitmap = BitmapManager.instance().getThumbnail(mContext.getContentResolver(), id,
							      Images.Thumbnails.MINI_KIND,options,false);
	   return bitmap;
	}

	public void cleanBitmapList(){
	    if (mReleaseBitMapList.size() <= 0) return;
	    for (Bitmap bitmap : mReleaseBitMapList) {
		    if (bitmap != null && !bitmap.isRecycled()) {
			    bitmap.recycle();    
		    }
	    }
	    
	    mReleaseBitMapList.clear();
	    
	}

}
